home *** CD-ROM | disk | FTP | other *** search
- /* ***** BEGIN LICENSE BLOCK *****
- *
- * Pearltrees add-on AMO, Copyright(C), 2009, Broceliand SAS, Paris, France
- * (company in charge of developing Pearltrees)
- *
- * This file is part of ΓÇ£Pearltrees add-on AMOΓÇ¥.
- *
- * Pearltrees add-on AMO is free software: you can redistribute it and/or modify it under the
- * terms of the GNU General Public License version 3 as published by the Free Software Foundation.
- *
- * Pearltrees add-on AMO is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
- * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- * See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along with Pearltrees add-on AMO.
- * If not, see <http://www.gnu.org/licenses/>
- *
- * ***** END LICENSE BLOCK ***** */
-
- /////////////////////////////////////////////////////////////////////////////////
- // Miscelaneous tools
- /////////////////////////////////////////////////////////////////////////////////
-
- var BRO_tools = {
-
- trim:function(myString) {
- if(!myString) return "";
- return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
- },
-
- removeFirefoxNameFromTitle:function(title) {
- return title.replace(/ - Mozilla Firefox/,"");
- },
-
- /**
- * Open a new tab with a specific URL.
- * @param string url
- */
- openAndReuseOneTabPerURL:function(url) {
- var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
- .getService(Components.interfaces.nsIWindowMediator);
- var browserEnumerator = wm.getEnumerator("navigator:browser");
-
- // Check each browser instance for our URL
- var found = false;
- while (!found && browserEnumerator.hasMoreElements()) {
- var browserInstance = browserEnumerator.getNext().getBrowser();
-
- // Check each tab of this browser instance
- var numTabs = browserInstance.tabContainer.childNodes.length;
- for(var index=0; index<numTabs; index++) {
- var currentBrowser = browserInstance.getBrowserAtIndex(index);
- if (currentBrowser.currentURI && url == currentBrowser.currentURI.spec) {
-
- // The URL is already opened. Select this tab.
- browserInstance.selectedTab = browserInstance.tabContainer.childNodes[index];
-
- // Focus *this* browser
- browserInstance.focus();
- found = true;
- break;
- }
- }
- }
- // Our URL isn't open. Open it now.
- if (!found) {
- var recentWindow = wm.getMostRecentWindow("navigator:browser");
- if (recentWindow) {
- // Use an existing browser window
- recentWindow.delayedOpenTab(url, null, null, null, null);
- }
- else {
- // No browser windows are open, so open a new one.
- window.open(url);
- }
- }
- },
-
- /**
- * Set the browser window's location to the incoming URL
- * @param string URL to show
- */
- openURLinCurrentTab:function(url, delay, browserID) {
- if(!delay) {
- var selectedBrowser = null;
- if(browserID) {
- selectedBrowser = BRO_browserManager.getBrowserByID(browserID);
- }
- if(!selectedBrowser) {
- selectedBrowser = BRO_browserManager.getSelectedBrowser();
- }
- selectedBrowser.contentDocument.location = url;
- }
- else{
- browserID = BRO_browserManager.getSelectedBrowserID();
- BRO_tools.callWithDelay("BRO_tools.openURLinCurrentTab('"+url+"',null,'"+browserID+"')",delay);
- }
- },
-
- callWithDelay:function(code, time, param) {
- // Having an issue with nsITimer, thus we use setTimeout
- setTimeout(code, time, param);
- },
-
- getTime:function() {
- var date = new Date();
- return date.getTime();
- },
-
- randomID:function(size) {
- var str = "";
- for(var i = 0; i < size; i++)
- {
- str += this.getRandomChar();
- }
- return str;
- },
-
- getRandomChar:function() {
- var chars = "0123456789abcdefghijklmnopqurstuvwxyzABCDEFGHIJKLMNOPQURSTUVWXYZ";
- return chars.substr( this.getRandomNumber(62), 1 );
- },
-
- getRandomCharWithSpace:function() {
- var chars = "0123456789 abcdefghijklmnop qur stuvwxyz ABCDEFGHIJ KLMNOPQURSTUVW XYZ";
- return chars.substr( this.getRandomNumber(68), 1 );
- },
-
- getRandomNumber:function(range) {
- return Math.floor(Math.random() * range);
- },
-
- /**
- * Return a random string with a specified chars lnumber.
- * @param integer num Number of chars
- * @return string
- */
- getRandomString:function(num) {
- var string = '';
- for(var i=0;i<num;i++) {
- string = string+BRO_tools.getRandomCharWithSpace();
- }
- return string;
- }
- }